<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>Timeline demo</title>

    <style>
		  body {font: 10pt verdana;}

		</style>

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript" src="../timeline.js"></script>
    <link rel="stylesheet" type="text/css" href="../timeline.css">
    
    <script type="text/javascript">
      google.load("visualization", "1");
      
      // Set callback to run when API is loaded
      google.setOnLoadCallback(drawVisualization); 
      
      var timeline;
      var data;
      
      function getSelectedRow() {
        var row = undefined
        var sel = timeline.getSelection();
        if (sel.length) {
          if (sel[0].row != undefined) {
            var row = sel[0].row;
          }
        }
        return row;       
      }
      
      // Called when the Visualization API is loaded.
      function drawVisualization() {
        // Create and populate a data table.
        data = new google.visualization.DataTable();
        data.addColumn('datetime', 'start');
        data.addColumn('datetime', 'end');
        data.addColumn('string', 'content');

        data.addRows([
          [new Date(2011,01,23), , '<div>Conversation</div><img src="img/comments-icon.png" style="width:32px; height:32px;">'],
          [new Date(2011,01,23,23,00,00), , '<div>Mail from boss</div><img src="img/mail-icon.png" style="width:32px; height:32px;">'],
          [new Date(2011,01,24,16,00,00), , 'Report'],
          [new Date(2011,01,26), new Date(2011,02,02), 'Traject A'],     
          [new Date(2011,01,27), , '<div>Memo</div><img src="img/notes-edit-icon.png" style="width:48px; height:48px;">'],
          [new Date(2011,01,29), , '<div>Phone call</div><img src="img/Hardware-Mobile-Phone-icon.png" style="width:32px; height:32px;">'],
          [new Date(2011,01,28), new Date(2011,02,03), 'Traject B'],     
          [new Date(2011,02,04,12,00,00), , '<div>Report</div><img src="img/attachment-icon.png" style="width:32px; height:32px;">']
        ]);

        // specify options
        options = {width:  "100%", 
                   //height: "300px", 
                   height: "auto", 
                   editable: true,   // enable dragging and editing events
                   enableKeys: true,
                   axisOnTop: false,
                   showNavigation: true,
                   layout: "box" 
                   };

        // Instantiate our timeline object.
        timeline = new links.Timeline(document.getElementById('mytimeline'));

        // Add event listeners
        google.visualization.events.addListener(timeline, 'select', onselect);
        google.visualization.events.addListener(timeline, 'change', onchange);
        google.visualization.events.addListener(timeline, 'add', onadd);
        google.visualization.events.addListener(timeline, 'edit', onedit);
        google.visualization.events.addListener(timeline, 'delete', ondelete);
        google.visualization.events.addListener(timeline, 'rangechange', onrangechange);
        google.visualization.events.addListener(timeline, 'rangechanged', onrangechanged);

        // Draw our timeline with the created data and options 
        timeline.draw(data, options);
        onrangechange();
      }

      // Make a callback function for the select event
      var onselect = function (event) {
        var row = getSelectedRow();   
        
        document.getElementById("info").innerHTML += "event " + row + " selected<br>";            
        // Note: you can retrieve the contents of the selected row with
        //       data.getValue(row, 2);
      }

      // callback function for the change event
      var onchange = function () {
        var row = getSelectedRow();   
        document.getElementById("info").innerHTML += "event " + row + " changed<br>";
      }

      // callback function for the delete event
      var ondelete = function () {
        var row = getSelectedRow();   
        document.getElementById("info").innerHTML += "event " + row + " deleted<br>";
      }
      
      // callback function for the edit event
      var onedit = function () {
        var row = getSelectedRow();   
        document.getElementById("info").innerHTML += "event " + row + " edit<br>";
        var content = data.getValue(row, 2);
        var newContent = prompt("Enter content", content);
        if (newContent != undefined) {
          data.setValue(row, 2, newContent);
        }
      }
              
      // callback function for the add event
      var onadd = function () {
        var row = getSelectedRow();   
        document.getElementById("info").innerHTML += "event " + row + " created<br>";
        var content = data.getValue(row, 2);
        var newContent = prompt("Enter content", content);
        if (newContent != undefined) {
          data.setValue(row, 2, newContent);
        }
        else {
          // cancel adding the event
          timeline.cancelAdd();
        }
      }
      
      function onrangechange() {
        // adjust the values of startDate and endDate
        var range = timeline.getVisibleChartRange();
        document.getElementById('startDate').value = dateFormat(range.start);
        document.getElementById('endDate').value   = dateFormat(range.end);
      }
                  
      function onrangechanged() {
        document.getElementById("info").innerHTML += "range changed<br>";
      }
      
      // adjust start and end time.
      function setTime() {
        if (!timeline) return;
        
        var newStartDate = new Date(document.getElementById('startDate').value);
        var newEndDate   = new Date(document.getElementById('endDate').value);
        timeline.setVisibleChartRange(newStartDate, newEndDate);     
      }      
      
      // set the visible range to the current time
      function setCurrentTime() {
        if (!timeline) return;
        
        timeline.setRangeToCurrentTime();
        onrangechange();
      }

      // Format given date as "yyyy-mm-dd hh:ii:ss"
      // @param datetime   A Date object.
      function dateFormat(date) {
        datetime =   date.getFullYear() + "-" + 
                   ((date.getMonth()   <  9) ? "0" : "") + (date.getMonth() + 1) + "-" +
                   ((date.getDate()    < 10) ? "0" : "") +  date.getDate() + " " +
                   ((date.getHours()   < 10) ? "0" : "") +  date.getHours() + ":" + 
                   ((date.getMinutes() < 10) ? "0" : "") +  date.getMinutes() + ":" + 
                   ((date.getSeconds() < 10) ? "0" : "") +  date.getSeconds()
        return datetime;
      }      
   </script>
   
  </head>

  <body>
    <p>This page demonstrates the timeline visualization.</p>
    <p>
    <ul>
      <li>Click and drag on the time axis to move the timeline, scroll to zoom the timeline</li>
      <li>Click and drag an event to change its date, double-click to change its text</li>
      <li>Click or drag on an empty spot in the timeline to create a new event</li>
    </ul>
    </p>
    <p>
      Starttime: <input type="text" id="startDate" value="">
      Endtime:   <input type="text" id="endDate" value="">
      <input type="button" id="setRange" value="Set" onclick="setTime();">
      <input type="button" id="setCurrentTime" value="Current time" onclick="setCurrentTime();">
    </p>
    
    <div id="mytimeline"></div>
    
    <!-- Information about where the used icons come from -->
    <p style="color:gray; font-size:10px; font-style:italic;">
      Icons by <a href="http://dryicons.com" target="_blank" title="Aesthetica 2 Icons by DryIcons" style="color:gray;" >DryIcons</a>
      and <a href="http://www.tpdkdesign.net" target="_blank" title="Refresh Cl Icons by TpdkDesign.net" style="color:gray;" >TpdkDesign.net</a>
    </p>
    
    <br>  
    <div id="info"></div>
  </body>
</html>
